package com.hy.mydesktop.client.mvc.logindialog;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;

public class LoginDialog extends Dialog {

	  public TextField<String> getUserNameTextField() {
			return userNameTextField;
		}

		public TextField<String> getPasswordTextField() {
			return passwordTextField;
		}

		public Button getLoginButton() {
			return loginButton;
		}

	protected TextField<String> userNameTextField;
	  protected TextField<String> passwordTextField;
	  protected Button reset;
	  protected Button loginButton;
	  protected Status status;

	  public LoginDialog() {
	    FormLayout layout = new FormLayout();
	    layout.setLabelWidth(90);
	    layout.setDefaultWidth(155);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    setIcon(IconHelper.createStyle("user"));
	    setHeading("GXT Application Login");
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	   //  setBodyStyle("padding: 8px;background: black");
	   // setBodyStyle("padding: 8px;background: resources/desktop/wallpapers/desktop.jpg");
	    setWidth(300);
	    setResizable(false);

	    KeyListener keyListener = new KeyListener() {
	      public void componentKeyUp(ComponentEvent event) {
	        validate();
	      }

	    };

	    userNameTextField = new TextField<String>();
	    userNameTextField.setMinLength(4);
	    userNameTextField.setFieldLabel("Username");
	    userNameTextField.setId("userName");
	    userNameTextField.addKeyListener(keyListener);
	    userNameTextField.setValue("user2222222");
	    add(userNameTextField);

	    passwordTextField = new TextField<String>();
	    passwordTextField.setMinLength(4);
	    passwordTextField.setPassword(true);
	    passwordTextField.setFieldLabel("Password");
	    passwordTextField.setId("password");
	    passwordTextField.addKeyListener(keyListener);
	    passwordTextField.setValue("user2222222");

	    add(passwordTextField);

	    setFocusWidget(userNameTextField);

	  }

	  @Override
	  protected void createButtons() {
	    super.createButtons();
	    status = new Status();
	    status.setBusy("please wait...");
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    reset = new Button("Reset");
	    reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	        userNameTextField.reset();
	        passwordTextField.reset();
	        validate();
	        userNameTextField.focus();
	      }

	    });

	    loginButton = new Button("Login");
	    loginButton.disable();
	    
	    

	    addButton(reset);
	    addButton(loginButton);

	    
	  }

	  

	  protected boolean hasValue(TextField<String> field) {
	    return field.getValue() != null && field.getValue().length() > 0;
	  }

	  protected void validate() {
	    loginButton.setEnabled(hasValue(userNameTextField) && hasValue(passwordTextField)
	        && passwordTextField.getValue().length() > 3);
	  }

	}

